/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-7-17 下午12:20 Created by hytsaber
 */
package com.jade.bss.business.scene.impl;

import java.util.*;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.bss.business.scene.SceneDataDao;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Group;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.criterion.sql.SqlContext;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.DataUtils;
import com.jade.framework.base.util.PagingArrayList;
import com.jade.framework.base.util.PagingList;
import com.jade.bss.business.scene.SceneData;

/**
 * TODO: Must write comments.
 *
 * @author <a href="mailto:hytsaber@ceno.cn">hytsaber</a>
 * @version 1.0 15-7-17 下午12:20
 */
@Named ("bss_sceneDataDao")
public class JpaSceneDataDao implements SceneDataDao
{

    @PersistenceContext (unitName = "default")
    protected EntityManager em;

    public PagingList<Map> query(Criteria criteria)
    {
        String sqlCount = "SELECT count(DISTINCT groupId) FROM SceneData";
        int count = getTotalCount(sqlCount,criteria);
        String sql = "SELECT groupId FROM SceneData";
        String sqlRecord = "SELECT sceneData FROM SceneData sceneData";
        Group group = new Group();
        group.addGroup(null, "groupId");
        criteria.setGroup(group);
        List<Object> groupIds = SqlUtils.sqlQueryWithoutCount(sql,em,criteria);
        List<Map> listMap = new ArrayList<Map>();
        for(Object object : groupIds)
        {
            Criteria criteriaRecord = new Criteria(-1,-1);
            criteriaRecord.add(Restrictions.eq("groupId", (String)object));
            List<SceneData> list = SqlUtils.sqlQueryWithoutCount(sqlRecord,em,criteriaRecord);
            Map map = new HashMap();
            Date date = null;
            for(SceneData sceneData : list)
            {
                if(map.get(sceneData.getTitle())==null)
                    map.put(sceneData.getTitle(),sceneData.getValue());
                date = sceneData.getCreateTime();
            }
            map.put("createTime",date);
            listMap.add(map);
        }
        return new PagingArrayList<Map>(count, listMap);
    }

    public String[] getColumns()
    {
        Criteria criteria = new Criteria(-1,-1);
        Group group = new Group();
        group.addGroup(null, "title");
        criteria.setGroup(group);
        String sql = "SELECT title FROM SceneData";
        List<Object> objects = SqlUtils.sqlQueryWithoutCount(sql,em,criteria);
        if(objects!=null&&objects.size()>0)
        {
            String[] columns = new String[objects.size()];
            for(int i=0;i<objects.size();i++)
            {
                columns[i] = (String)objects.get(i);
            }
            return columns;
        }
        else
            return null;
    }

    public long add(SceneData sceneData)
    {
        em.persist(sceneData);
        return sceneData.getId();
    }

    protected int getTotalCount(String sql, Criteria criteria)
    {
        SqlContext context = new SqlContext(sql);
        SqlUtils.interpreteSql(context, criteria.getCriterions());
        String countSql = context.getContextString();
        Query query = em.createQuery(countSql);
        for (int i = 0; i < context.getPreparedValues().size(); i++) {
            query.setParameter(i + 1, context.getPreparedValues().get(i));
        }
        return DataUtils.getInt(query.getSingleResult(), 0);
    }
}
